package com.ruoyi.sinatv.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/sinatv")
@CrossOrigin
public class SinatvController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Value("${tx.live.key}")
    private String key;

    /**
     * 获取当前登录人
     */
    @GetMapping("/goSinatv")
    public void goSinatv() {
        String username = SecurityUtils.getUsername();
        redisTemplate.opsForValue().set("username", username);

    }


    @GetMapping("/begin")
    public AjaxResult begin(){
        String username = redisTemplate.opsForValue().get("username");
        //生成直播秘钥，组装推流地址
        //获取系统当前时间+有效期 = 链接有效期截止时间===》16进制
        long time = System.currentTimeMillis()+24*60*60*1000;
        String txTime = Long.toHexString(time / 1000).toUpperCase();
        //MD5(key + txSecret + txTime)
        String txSecret= DigestUtils.md5Hex(key+username+txTime);

        String path= "webrtc://182177.push.tlivecloud.com/shanpay/"+username+"?txSecret="+txSecret+"&txTime="+txTime;
        System.out.println("=================="+path);
        return AjaxResult.success(path);
    }

}
